package server

import (
	"gitee.com/sdynasty/clever/clog"
	"gitee.com/sdynasty/clever/socket"
)

var (
	MQ chan socket.ChanMessage
	closeChan chan bool
	isProtoBuf bool
	//handler MessageHandler
)

//type chanMessage struct {
//	fd uint32
//	data []byte
//}

// 消息处理接口
type MessageHandler interface {
	OnMessage(uint32, []byte)
	OnNetworkBroken(uint32)
}

func StartMQ(h MessageHandler) {
	var(
		cm socket.ChanMessage
		//err error
	)

	MQ = make(chan socket.ChanMessage, 1000)
	closeChan = make(chan bool, 1)
	isProtoBuf = isProtoBuf
	//handler = h

	for {
		select{
		case cm = <-MQ:
		case <-closeChan:
			goto ERR
		}

		clog.LogNetwork().Debugf("MQ receive fd:%d, dataType:%d, len:%d\n", cm.Fd, cm.DataType, len(cm.Data))
		if cm.DataType == socket.SocketDataType_NetBroken {
			h.OnNetworkBroken(cm.Fd)
		} else if h != nil {
			h.OnMessage(cm.Fd, cm.Data)
		}
	}

ERR:
}




func CloseMQ() {
	closeChan <- true
}